package org.qloudgen.txn.dilithium.cluster.pages.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.txn.dilithium.cluster.operator.ClusterOperatorImpl;
import org.qloudgen.txn.dilithium.cluster.operator.IClusterOperator;
import org.qloudgen.txn.dilithium.cluster.pages.ClusterMainPage;

import org.qloudgen.txn.dilithium.cluster.beans.Cluster;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.database.ClusterImpl;

public class ConfirmPage extends BasePage{

	/**
	 * 
	 */
	static Log log =  LogFactory.getLog(ConfirmPage.class);
	private static final long serialVersionUID = 1L;
	private PageParameters pp ;
	
	public ConfirmPage(PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
		
		final String buttonName = this.pp.get("buttonName").toString();
		final int clusterID = this.pp.get("clusterID").toInt();
		
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		add(feedback);
				
		if (buttonName.equals("start")){
			
			info(CommonValues.INFO_START);
			
		} else if (buttonName.equals("stop")){
			
			info(CommonValues.INFO_STOP);
			
		} else if (buttonName.equals("delete")){
			
			info(CommonValues.INFO_DELETE);
			
		}
		
		Form<?> form = new Form<Object>("form");
		add(form);

		final Button confirm = new Button ("confirm"){

			private static final long serialVersionUID = 1L;
			@Override
			public void onSubmit() {
				
				Cluster cluster = ClusterImpl.getClusterInfo(clusterID);
				
				IClusterOperator operator = new ClusterOperatorImpl(cluster,buttonName);
				int result = operator.handler();
				if (result == -1) 
					info(CommonValues.ERROR);
				else 
					setResponsePage(ClusterMainPage.class,pp);
			}
		};
		
		confirm.setDefaultFormProcessing(true);
		form.add(confirm);
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			@Override
			public void onSubmit() {
				setResponsePage(ClusterMainPage.class,pp);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
		
		addFooter();
	}

}
